package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("spec")
public class SpecController {

    private SpecGroupService groupService;
    private SpecParamService paramService;

    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
        this.groupService = groupService;
        this.paramService = paramService;
    }

    /**
     * 新增规格组
     * @param specGroupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        this.groupService.addSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param specGroupDTO
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        this.groupService.updateSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据商品分类ID,查询规格组集合
     * @param cid
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> listSpecGroupByCid(@RequestParam("id") Long cid){
        return ResponseEntity.ok(this.groupService.listSpecGroupByCid(cid));

    }
    /**
     * 根据条件查询规格参数集合
     * @param cid
     * @param gid
     * @param searching
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> listSpecParam(@RequestParam(value = "categoryId",required = false)Long cid,
                                                            @RequestParam(value = "groupId",required = false)Long gid,
                                                            @RequestParam(value = "searching",required = false)Boolean searching
                                                            ){
        return ResponseEntity.ok(this.paramService.listSpecParam(cid,gid,searching));

    }

    /**
     * 根据分类id查询规格组及组内参数
     * @param cid
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> lisSpecGroup(@RequestParam("id")Long cid){
        return ResponseEntity.ok(this.groupService.listSpecGroup(cid));

    }

    /**
     * 修改规格参数
     * @param specParamDTO
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO){
        this.paramService.updateSpecGroup(specParamDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    @PostMapping("/param")
    public ResponseEntity<Void> addSpecParam(@RequestBody SpecParamDTO specParamDTO){
        this.paramService.addSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 删除规格组
     * @param specGroupDTO
     * @return
     */
    @Delete("/param")
    public ResponseEntity<Void> deleteSpecGroup(@RequestBody SpecGroupDTO specGroupDTO){
        this.groupService.deleteSpecGroup(specGroupDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    @Delete("/param")
    public ResponseEntity<Void> deleteSpecParam(@RequestBody SpecParamDTO specParamDTO){
        this.paramService.deleteSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }



}
